{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0e9aa3ea",
   "metadata": {},
   "source": [
    "# 第12周\n",
    "* author:邱星倩\n",
    "* time：第12周周二上午\n",
    "* link:[语音识别](https://ai.baidu.com/tech/speech)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb979574",
   "metadata": {},
   "source": [
    "# 本周内容：\n",
    "> 1.关于麦克风的安装    \n",
    "> 2.优化ASR（自动语音识别）机器人    \n",
    "> 3.连接图灵机器人    \n",
    "> 4.语音合成    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5eda4df",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee849668",
   "metadata": {},
   "source": [
    "# 1. 麦克风安装"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26b546c2",
   "metadata": {},
   "source": [
    "* 安装 speech_recognition\n",
    ">> pip install SpeechRecognition  \n",
    "* 安装 PyAudio\n",
    ">> Not found name \"PyAudio\" moudle  \n",
    ">> pip install pipwin  \n",
    ">> pipwin install PyAudio  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68c2d290",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5eb96ba8",
   "metadata": {},
   "source": [
    "# 2.优化ASR--语音识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "057924a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'OYDEhX1ETdUGqrU74FkOLiYh'\n",
    "SECRET_KEY = '54r27F28gYrzIOdxYuxkhOt2GgcDe6dY'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "aa5af878",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "dd2f190c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.83fc16649621f6c3f8eb5ba21cb67cc0.2592000.1655900326.282335-25321929 ; EXPIRES IN SECONDS: 2592000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'24.83fc16649621f6c3f8eb5ba21cb67cc0.2592000.1655900326.282335-25321929'"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取fetch_token\n",
    "asr_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26155c58",
   "metadata": {},
   "source": [
    "## 使用asr语音识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c778884e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "\n",
    "with open(\"5-23.wav\",\"wb\")as f:\n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4eae507d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.814073\n",
      "{\"corpus_no\":\"7100825846441271772\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好呀。\"],\"sn\":\"465721173751653289852\"}\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7100825846441271772\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好呀。\"],\"sn\":\"465721173751653289852\"}\\n'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr(asr_token,'5-23.wav')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24e24445",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00686b4c",
   "metadata": {},
   "source": [
    "# 3.连接图灵机器人 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "863c7484",
   "metadata": {},
   "source": [
    "* API V2.0 基于图灵机器人平台语义理解、 深度学习等核心技术"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "974e33f3",
   "metadata": {},
   "source": [
    "## 尝试连接机器人\n",
    "\n",
    "* 接口地址：\n",
    "http://openapi.turingapi.com/openapi/api/v2\n",
    "    \n",
    "* 请求方式：\n",
    "post\n",
    "\n",
    "* 请求参数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "9e6361c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_KEY = \"d5d5bb66f66d4da8bedb378df7adfa70\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "8b777d1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "38fbf9cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "def TL_API(key,text):\n",
    "    TL_URL = \"http://openapi.turingapi.com/openapi/api/v2\"\n",
    "\n",
    "    payload = {\n",
    "        \"reqType\":0,\n",
    "         \"perception\": {\n",
    "            \"inputText\": {\n",
    "                \"text\": text\n",
    "            },\n",
    "         },\n",
    "\n",
    "        \"userInfo\": {\n",
    "            \"apiKey\": TL_KEY,\n",
    "            \"userId\": \"00001\"\n",
    "        }\n",
    "    }\n",
    "\n",
    "    res = requests.post(TL_URL,data = json.dumps(payload))\n",
    "\n",
    "    return res.json()['results'][0]['values']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4816ad77",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f974ac7",
   "metadata": {},
   "source": [
    "## ----> 尝试：对话机器人"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "614eb53a",
   "metadata": {},
   "source": [
    "* 1. 录制音频\n",
    "* 2. 语音识别--（百度ASR接口）——音频转文本信息\n",
    "* 3. 图灵API——{进行文字输入和回复}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "e2ebaa07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.c615b00615cd2b91546e816df9432bb2.2592000.1655900791.282335-25321929 ; EXPIRES IN SECONDS: 2592000\n",
      "Request time cost 0.410291\n",
      "{\"corpus_no\":\"7100907193201149110\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"今天的广州天气。\"],\"sn\":\"4013553351653308792\"}\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'text': 'apple'}"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.录制音频\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "\n",
    "with open(\"5-23.wav\",\"wb\")as f:\n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "# 2.语音识别\n",
    "asr_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_output = eval(asr(asr_token,'5-23.wav'))['result'][0]\n",
    "\n",
    "\n",
    "# 3.图灵机器人\n",
    "TL_API(TL_KEY,asr_output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39b59ce0",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "966873b6",
   "metadata": {},
   "source": [
    "# 4.语音合成（文字输出音频）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d00bfd4",
   "metadata": {},
   "source": [
    "## 语音合成测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "c7538f78",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import json\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "if IS_PY3:\n",
    "    from urllib.request import urlopen\n",
    "    from urllib.request import Request\n",
    "    from urllib.error import URLError\n",
    "    from urllib.parse import urlencode\n",
    "    from urllib.parse import quote_plus\n",
    "else:\n",
    "    import urllib2\n",
    "    from urllib import quote_plus\n",
    "    from urllib2 import urlopen\n",
    "    from urllib2 import Request\n",
    "    from urllib2 import URLError\n",
    "    from urllib import urlencode\n",
    "\n",
    "TEXT = \"欢迎欢迎热烈欢迎红红火火恍恍惚惚。\"\n",
    "\n",
    "# 发音人选择, 基础音库：0为度小美，1为度小宇，3为度逍遥，4为度丫丫，\n",
    "# 精品音库：5为度小娇，103为度米朵，106为度博文，110为度小童，111为度小萌，默认为度小美 \n",
    "PER = 4\n",
    "# 语速，取值0-15，默认为5中语速\n",
    "SPD = 5\n",
    "# 音调，取值0-15，默认为5中语调\n",
    "PIT = 5\n",
    "# 音量，取值0-9，默认为5中音量\n",
    "VOL = 5\n",
    "# 下载的文件格式, 3：mp3(default) 4： pcm-16k 5： pcm-8k 6. wav\n",
    "AUE = 6\n",
    "\n",
    "FORMATS = {3: \"mp3\", 4: \"pcm\", 5: \"pcm\", 6: \"wav\"}\n",
    "FORMAT = FORMATS[AUE]\n",
    "\n",
    "CUID = \"123456PYTHON\"\n",
    "\n",
    "TTS_URL = 'http://tsn.baidu.com/text2audio'\n",
    "\n",
    "\n",
    "class DemoError(Exception):\n",
    "    pass\n",
    "\n",
    "\n",
    "\n",
    "# 语音合成-tts\n",
    "def tts(token,TEXT):\n",
    "\n",
    "    tex = quote_plus(TEXT)  # 此处TEXT需要两次urlencode\n",
    "    params = {'tok': token, 'tex': tex, 'per': PER, 'spd': SPD, 'pit': PIT, 'vol': VOL, 'aue': AUE, 'cuid': CUID,\n",
    "              'lan': 'zh', 'ctp': 1}  # lan ctp 固定参数\n",
    "\n",
    "    data = urlencode(params)\n",
    "\n",
    "\n",
    "    req = Request(TTS_URL, data.encode('utf-8'))\n",
    "    has_error = False\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "\n",
    "        headers = dict((name.lower(), value) for name, value in f.headers.items())\n",
    "\n",
    "        has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)\n",
    "    except  URLError as err:\n",
    "\n",
    "        result_str = err.read()\n",
    "        has_error = True\n",
    "\n",
    "    save_file = \"error.txt\" if has_error else 'result.' + FORMAT\n",
    "    with open(save_file, 'wb') as of:\n",
    "        of.write(result_str)\n",
    "\n",
    "    if has_error:\n",
    "        if (IS_PY3):\n",
    "            result_str = str(result_str, 'utf-8')\n",
    "\n",
    "\n",
    "    print(\"result saved as :\" + save_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "018503d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "tts(tts_token,TEXT)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c40f713",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79e43577",
   "metadata": {},
   "source": [
    "## 在jupyter中调用音频模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "9c8ac301",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pydub\n",
      "  Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
      "Installing collected packages: pydub\n",
      "Successfully installed pydub-0.25.1\n"
     ]
    }
   ],
   "source": [
    "!pip install pydub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "b4eca8f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03ee9afd",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "051bc5c1",
   "metadata": {},
   "source": [
    "## 4.1 作业：接入对话机器人流程\n",
    "\n",
    "> 1.录制音频  \n",
    "> 2.语音识别--（百度ASR接口）——音频转文本信息  \n",
    "> 3.图灵API--进行文字输入和文字回复  \n",
    "> 4.语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "684dac72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.dd5d7c77d8dd5667cd8da488142bcb2e.2592000.1655900813.282335-25321929 ; EXPIRES IN SECONDS: 2592000\n",
      "Request time cost 0.464643\n",
      "{\"corpus_no\":\"7100907283115290938\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"今天的广州天气。\"],\"sn\":\"945693439861653308813\"}\n",
      "\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "# 1.录制音频\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "\n",
    "with open(\"5-23.wav\",\"wb\")as f:\n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "# 2.语音识别\n",
    "asr_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_output = eval(asr(asr_token,'5-23.wav'))['result'][0]\n",
    "\n",
    "\n",
    "# 3.图灵机器人\n",
    "TL_result = TL_API(TL_KEY,asr_output)['text']\n",
    "\n",
    "# 4.语音合成\n",
    "tts(asr_token,TL_result)\n",
    "\n",
    "# 5.语音对话\n",
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e4969e8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
